﻿using System;
using System.Windows;
using System.Windows.Forms;
using UtilZ.Dotnet.Ex.Base;
using UtilZ.Dotnet.WindowsDesktopEx.Base;

namespace UtilZ.Dotnet.WindowsDesktopEx.WPF
{
    /// <summary>
    /// NotifyWindow.xaml 的交互逻辑
    /// </summary>
    public partial class NotifyWindow : Window
    {
        private const double _PRE = 0.001d;

        private readonly NotifyItem _item;
        private readonly int _padding;
        private int _animalXOffset;
        private readonly double _opacityStep = -1;
        private NotifyStatus _notifyStatus = NotifyStatus.Starting;
        private bool _clickCloseButton = false;
        private bool _isMouseHover = false;

        /// <summary>
        /// 
        /// </summary>
        public NotifyWindow()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="item"></param>
        /// <param name="opacityStep"></param>
        /// <param name="padding"></param>
        public NotifyWindow(NotifyItem item, double opacityStep, int padding)
            : this()
        {
            this._item = item;
            if (item.BackgroundImage != null)
            {
                gridRoot.Background = item.BackgroundImage;
            }
            else
            {
                gridRoot.Background = item.BackgroundColor;
            }

            if (item.Size.HasValue)
            {
                this.Width = item.Size.Value.Width;
                this.Height = item.Size.Value.Height;
            }

            imgMsgType.Source = item.MesageImage;
            tbMsg.Text = item.Text;
            imgClose.Source = NotifyManager.CloseImg;

            this._opacityStep = opacityStep;
            if (opacityStep > 0)
            {
                this.Opacity = 0.0d;
            }

            //this.TopMost = true;

            this._padding = padding;
            this._animalXOffset = padding;
        }

        private long _startTimestamp = -1;
        internal bool Check()
        {
            switch (this._notifyStatus)
            {
                case NotifyStatus.Starting:
                    if (this._startTimestamp < 0)
                    {
                        this._startTimestamp = TimeEx.GetTimestamp();
                    }

                    if (this._opacityStep > 0)
                    {
                        if (this._animalXOffset > 0)
                        {
                            this.Left = Screen.PrimaryScreen.WorkingArea.Width - this._padding - this.Width + this._animalXOffset--;
                        }

                        if (1.0d - this.Opacity < _PRE)
                        {
                            if (this.Opacity < 1.0d)
                            {
                                this.Opacity = 1.0d;
                            }

                            this._notifyStatus = NotifyStatus.Wait;
                        }
                        else
                        {
                            var opacity = this.Opacity + this._opacityStep;
                            if (opacity - 1.0d > _PRE)
                            {
                                opacity = 1.0d;
                                this._notifyStatus = NotifyStatus.Wait;
                            }
                            this.Opacity = opacity;
                        }
                    }
                    else
                    {
                        this._notifyStatus = NotifyStatus.Wait;
                    }
                    break;
                case NotifyStatus.Wait:
                    if (TimeEx.GetTimestamp() - this._startTimestamp > this._item.Duration)
                    {
                        this._notifyStatus = NotifyStatus.Closing;
                    }
                    break;
                case NotifyStatus.Closing:
                    if (this._clickCloseButton)
                    {

                    }
                    else
                    {
                        if (this._isMouseHover)
                        {
                            if (this.Opacity < 1.0d)
                            {
                                this.Opacity = 1.0d;
                            }

                            return false;
                        }
                    }

                    if (this._opacityStep > 0)
                    {
                        //有动画就慢慢消失并关闭
                        this.Left = Screen.PrimaryScreen.WorkingArea.Width - this._padding - this.Width + this._animalXOffset++;

                        var opacity = this.Opacity - this._opacityStep;
                        if (opacity < _PRE)
                        {
                            this.Opacity = 0.0d;
                            this.Close();
                            return true;
                        }

                        this.Opacity = opacity;
                    }
                    else
                    {
                        //无动画直接关闭
                        this.Close();
                    }

                    return false;
            }

            return false;
        }

        internal void UpdateLocaion(double totalHeight, int margin)
        {
            this.Left = Screen.PrimaryScreen.WorkingArea.Width - this._padding - this.ActualWidth + this._animalXOffset;
            this.Top = Screen.PrimaryScreen.WorkingArea.Height - totalHeight - this.ActualHeight - margin;
        }

        private void imgClose_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            this._notifyStatus = NotifyStatus.Closing;
            this._clickCloseButton = true;
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            this._item.Closed?.Invoke(this._item);
        }

       

        private void Window_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            this._isMouseHover = true;
        }

        private void Window_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            this._isMouseHover = false;
        }


        private void OnRaiseMouseClick()
        {
            this._item.MouseClick?.Invoke(this._item);
        }


        private void imgMsgType_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            this.OnRaiseMouseClick();
        }

        private void borderMsg_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            this.OnRaiseMouseClick();
        }

    }
}
